package com.dankegongyu.component.jsbridge.config;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 2018/5/7.
 */
public class DKWebServiceManager {

    private String DEFAULT_KEY = "_DEFAULT_KEY";

    private static DKWebServiceManager mInstance = new DKWebServiceManager();

    private DKWebServiceManager() {
    }

    public static DKWebServiceManager getInstance() {
        return mInstance;
    }

    private Map<String, DKWebService> mContainer = new HashMap<>();

    public DKWebService get(String key) {

        if (TextUtils.isEmpty(key)) {
            DKWebService service = DKWebService.create();
            mContainer.put(DEFAULT_KEY, service);
            return service;
        }

        DKWebService service = mContainer.get(key);
        if (service == null) {
            service = DKWebService.create();
            mContainer.put(key, service);
        }

        return service;

    }

    public boolean clear(String key) {

        if (TextUtils.isEmpty(key)) {
            DKWebService service = mContainer.remove(DEFAULT_KEY);
            if (service != null) {
                service.unregister();
            }
            return true;
        }

        DKWebService service = mContainer.remove(key);
        if (service != null) {
            service.unregister();
        }
        return true;
    }


}
